package forms;

import helpers.DateHelper;

import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import models.UserModel;

import entities.User;

public class UserForm {
	public static TreeMap checkValid(HttpServletRequest request, User user) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();

		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		String repassword = (String) request.getParameter("rePassword");
		String name = (String) request.getParameter("name");
		String gender = (String) request.getParameter("gender");
		
		if (username == null || username.isEmpty()) {
			check.put("username", "Username is not empty!");
			user.setUsername("");
		} else {
			// Check user exists
			UserModel userModel = UserModel.getInstance();
			if (userModel.checkUserExists(username)) {
				check.put("username", "Username has been registed. Please choise other username");
			} else {
				Pattern p = Pattern
						.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
				Matcher m = p.matcher(username);
				if (!m.matches()) {
					check.put("username", "Username must be email!");
				}
			}
			user.setUsername(username);
		}

		if (name == null || name.isEmpty()) {
			check.put("name", "Name is not emtpy!");
			user.setName(name);
		} else {
			user.setName(name);
		}

		if (gender != null) {
			try {
				user.setGender(Integer.parseInt(gender));
			} catch (Exception e) {
				check.put("gender", "Gender is not valid!");
			}
		}

		if (password == null || password.isEmpty()) {
			check.put("password", "Password is not emtpy!");
			user.setPassword("");
		} else {
			if (repassword == null || repassword.isEmpty()) {
				check.put("rePassword",
						"RePassword is not empty.");
				user.setRePassword("");
			} else if (!password.equalsIgnoreCase(repassword)) {
				check.put("rePassword", "Password and RePassword is not same. Please enter again.");
				user.setRePassword(repassword);
			}
			user.setPassword(password);
			user.setRePassword(repassword);
		}

		form.put("checkValid", check);
		form.put("user", user);

		return form;
	}
	
	public static TreeMap checkValidProfile(HttpServletRequest request, User user) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String examPassword = "12345678";
		
		String password = (String) request.getParameter("password");
		String repassword = (String) request.getParameter("rePassword");
		String name = (String) request.getParameter("name");
		String gender = (String) request.getParameter("gender");
		String birthday = (String) request.getParameter("birthday");

		if (name == null || name.isEmpty()) {
			check.put("name", "Username is not empty.");
			user.setName(name);
		} else {
			user.setName(name);
		}

		if (gender != null) {
			try {
				user.setGender(Integer.parseInt(gender));
			} catch (Exception e) {
				check.put("gender", "Gender is not valid!");
			}
		}

		if (!password.equalsIgnoreCase(examPassword) || repassword.equalsIgnoreCase(examPassword)) {
			if (password == null || password.isEmpty()) {
				check.put("password", "Password is not emtpy!");
				user.setPassword("");
			} else {
				if (repassword == null || repassword.isEmpty()) {
					check.put("rePassword",
							"RePassword is not emtpy!");
					user.setRePassword("");
				} else if (!password.equalsIgnoreCase(repassword)) {
					check.put("rePassword",
							"Password and RePassword is not same. Please enter again.");
					user.setRePassword(repassword);
				}
				user.setPassword(password);
				user.setRePassword(repassword);
			}
		} else {
			user.setPassword(password);
			user.setRePassword(repassword);
		}

		/*if (birthday == null || !DateHelper.isValidDate(birthday)) {
			check.put("birthday", "Ngày sinh không hợp lệ.");
			user.setBirthday(DateHelper.StringToDate(birthday, "yyyy-MM-dd"));
		} else {
			user.setBirthday(DateHelper.StringToDate(birthday, "yyyy-MM-dd"));
		}*/

		form.put("checkValid", check);
		form.put("user", user);

		return form;
	}
	
	public static TreeMap checkLogin(HttpServletRequest request, User user) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();

		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");

		if (username == null || username.isEmpty()) {
			check.put("username", "Username is not empty.");
			user.setUsername("");
		} else {
			user.setUsername(username);
		}

		if (password == null || password.isEmpty()) {
			check.put("password", "Password is not empty.");
			user.setPassword("");
		} else {
			user.setPassword(password);
		}

		form.put("checkValid", check);
		form.put("user", user);

		return form;
	}
}
